﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace WorkOrder.Models;

/// <summary>
/// 工单主表
/// </summary>
[Table("工单主表")]
[Index("类型id", Name = "fk_工单_类型")]
[Index("父工单id", Name = "fk_父工单")]
[Index("优先级", Name = "idx_优先级")]
[Index("创建人id", Name = "idx_创建人")]
[Index("根工单id", "父工单id", Name = "idx_工单树")]
[Index("当前状态", Name = "idx_状态")]
[Index("工单编号", Name = "工单编号", IsUnique = true)]
public partial class 工单主表
{
    /// <summary>
    /// 工单ID
    /// </summary>
    [Key]
    [Column("工单ID")]
    [StringLength(50)]
    public string 工单id { get; set; } = Guid.NewGuid().ToString();

    /// <summary>
    /// 工单编号
    /// </summary>
    [StringLength(50)]
    public string 工单编号 { get; set; } = null!;

    /// <summary>
    /// 工单标题
    /// </summary>
    [StringLength(100)]
    public string 工单标题 { get; set; } = null!;

    /// <summary>
    /// 问题描述
    /// </summary>
    [Column(TypeName = "text")]
    public string 问题描述 { get; set; } = null!;

    /// <summary>
    /// 当前状态
    /// </summary>
     [StringLength(20)]
    public WorkOrderStatus 当前状态 { get; set; } = WorkOrderStatus.待处理;

    /// <summary>
    /// 优先级
    /// </summary>
    [StringLength(20)]
    public PriorityLevel 优先级 { get; set; } = PriorityLevel.正常; // 直接使用枚举类型

    /// <summary>
    /// 工单类型
    /// </summary>
    [Column("类型ID")]

    public ushort 类型id { get; set; }=1;

    /// <summary>
    /// 创建人
    /// </summary>
    [Column("创建人ID")]
    [StringLength(50)]
    public string 创建人id { get; set; }

    // /// <summary>
    // /// 责任部门
    // /// </summary>
    // [Column("责任部门ID")]
    // public uint? 责任部门id { get; set; }

    /// <summary>
    /// 父工单ID
    /// </summary>
    [Column("父工单ID")]
    [StringLength(50)]
    public string? 父工单id { get; set; }

    /// <summary>
    /// 根工单ID
    /// </summary>
    [Column("根工单ID")]
    [StringLength(50)]
    public string? 根工单id { get; set; }

    /// <summary>
    /// 截止时间
    /// </summary>
    [Column(TypeName = "datetime")]
    public DateTime? 截止时间 { get; set; } = DateTime.Now.AddMinutes(30);
 
    /// <summary>
    /// 创建时间
    /// </summary>
    [Column(TypeName = "datetime")]
    public DateTime 创建时间 { get; set; }

    /// <summary>
    /// 更新时间
    /// </summary>
    [Column(TypeName = "datetime")]
    public DateTime 更新时间 { get; set; }

    /// <summary>
    /// 关闭时间
    /// </summary>
    [Column(TypeName = "datetime")]
    public DateTime? 关闭时间 { get; set; }
    


    // 核心字段冗余存储（根据模板配置动态映射）
    [Column(TypeName = "decimal(18,2)")]
    public decimal? 总金额 { get; set; }

// 动态字段存储
    [Column(TypeName = "json")] // SQLite/MySQL通用配置
    public Dictionary<string, object> 扩展字段 { get; set; } = new();
    

    [Column("模板ID")]
    [StringLength(20)]
    public string 模板ID { get; set; }
    // 导航属性
    [ForeignKey("模板ID")]
    [InverseProperty("关联工单")]
    public virtual 工单模板表 关联模板 { get; set; }

    public bool 是否显示 { get; set; } = true;

    [InverseProperty("根工单")]
    public virtual ICollection<工单主表> Inverse根工单 { get; set; } = new List<工单主表>();

    [InverseProperty("父工单")]
    public virtual ICollection<工单主表> Inverse父工单 { get; set; } = new List<工单主表>();

    [InverseProperty("工单")]
    public virtual ICollection<工单分派记录表> 工单分派记录表s { get; set; } = new List<工单分派记录表>();

    [InverseProperty("工单")]
    public virtual ICollection<工单操作日志表> 工单操作日志表s { get; set; } = new List<工单操作日志表>();

    [InverseProperty("工单")]
    public virtual ICollection<工单评论表> 工单评论表s { get; set; } = new List<工单评论表>();

    [InverseProperty("工单")]
    public virtual ICollection<工单附件表> 工单附件表s { get; set; } = new List<工单附件表>();

    [ForeignKey("根工单id")]
    [InverseProperty("Inverse根工单")]
    public virtual 工单主表? 根工单 { get; set; }

    [ForeignKey("父工单id")]
    [InverseProperty("Inverse父工单")]
    public virtual 工单主表? 父工单 { get; set; }

    [ForeignKey("类型id")]
    [InverseProperty("工单主表s")]
    public virtual 工单类型表 类型 { get; set; } = null!;

 
}
